package com.luckymore.ydd.app.net;

import android.graphics.Bitmap;

import com.base.asynctaks.BitmapToBase64Task;
import com.base.commons.verify.MD5;
import com.base.control.MActivitySupport;
import com.base.control.RequestCallback;
import com.base.control.TextParamCheck;
import com.base.http.ApiClient;
import com.base.http.ParamsFormat;
import com.base.http.ParamsFormatFactory;
import com.base.util.ConstantResource;
import com.base.util.DateUtil;
import com.base.util.MLog;
import com.base.util.StringUtil;
import com.luckymore.ydd.app.model.BCourseList;
import com.luckymore.ydd.app.model.MAppEssence.MEssenceList;
import com.luckymore.ydd.app.model.MAppQuestion;
import com.luckymore.ydd.app.model.MAppUser;
import com.luckymore.ydd.app.model.MDays;
import com.luckymore.ydd.app.model.MRecommendKeys;
import com.luckymore.ydd.app.model.MSystem;
import com.luckymore.ydd.app.model.TrackByDay;
import com.luckymore.ydd.app.util.AppConfig;

import java.io.IOException;

public class ApiClientFactory {
    private static String Android = "android";

    //上传图片
    public static void uploadImg(final MActivitySupport context, final RequestCallback controller, final String methodName, final Bitmap bm, final String filename, int flag) throws IOException {
        try {
            final ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MImgUpload");
            final MSystem.MRet list = new MSystem.MRet();
            new BitmapToBase64Task(new BitmapToBase64Task.BitmapToBase64Callback() {
                @Override
                public void callback(String result) {
                    pf.addParam("img", result);
                    pf.addParam("filename", filename);
                    ApiClient.getApiData(context, controller, methodName, pf, list, false);
                }
            }, flag).execute(bm);
        } catch (Exception e) {
            MLog.e(e);
        }
    }

    //上传图片
    public static void uploadImg(MActivitySupport context, RequestCallback controller, String methodName, String img, String filename) {
        try {
            final ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MImgUpload");
            final MSystem.MRet list = new MSystem.MRet();
            pf.addParam("img", img);
            pf.addParam("filename", filename);
            ApiClient.getApiData(context, controller, methodName, pf, list, false);
        } catch (Exception e) {
            MLog.e(e);
        }
    }

    /*
	 * 获取精华列表
	 */
    public static void getEssenceListApi(MActivitySupport context, RequestCallback controller, String methodName, int page, int limit, int type, String key) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MEssenceList", page, limit);
        MEssenceList list = new MEssenceList();
        pf.addParam("subType", type);
        pf.addParam("key", key);
        ApiClient.getApiData(context, controller, methodName, pf, list);
    }

    //
    public static void getCollectionList(MActivitySupport context, RequestCallback controller, String methodName, int page, int limit, int type) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MMyEssences", page, limit);
        MEssenceList list = new MEssenceList();
        if (type != -1)
            pf.addParam("resType", type);
        ApiClient.getApiData(context, controller, methodName, pf, list);
    }

    public static void firstUser(MActivitySupport context, RequestCallback controller,
                             String methodName) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MGetWelcomePageAndroid");
        MAppUser.MUserList mUser = new MAppUser.MUserList();
        pf.addParam("device", Android);
        ApiClient.getApiData(context, controller, methodName, pf, mUser, false);
    }

    public static void login(MActivitySupport context, RequestCallback controller,
                             String methodName, String phone,
                             String password, boolean isEncrypt) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MLogin");
        MAppUser.MUser mUser = new MAppUser.MUser();
        if(TextParamCheck.PHONE_NUMBER_CHECKER.check(phone))
            pf.addParam("phone", phone);
        else {
            pf.addParam("account", phone);
        }
        if(!isEncrypt) {
            password = MD5.md5(ConstantResource.MD5_KEY + password);
        }
        pf.addParam("password", password);
        pf.addParam("device", Android);
        ApiClient.getApiData(context, controller, methodName, pf, mUser);
    }

    public static void loginByQQ(MActivitySupport context, RequestCallback controller,
                                 String methodName, String qqAccount) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MLogin");
        MAppUser.MUser mUser = new MAppUser.MUser();
        pf.addParam("device", "android");
        pf.addParam("qqAccount", qqAccount);
        ApiClient.getApiData(context, controller, methodName, pf, mUser);
    }

    public static void loginByWx(MActivitySupport context, RequestCallback controller,
                                 String methodName, String wxAccount) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MLogin");
        MAppUser.MUser mUser = new MAppUser.MUser();
        pf.addParam("device", "android");
        pf.addParam("wxAccount", wxAccount);
        ApiClient.getApiData(context, controller, methodName, pf, mUser);
    }

    public static void loginByWb(MActivitySupport context, RequestCallback controller,
                                 String methodName, String wbAccount) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MLogin");
        MAppUser.MUser mUser = new MAppUser.MUser();
        pf.addParam("device", "android");
        pf.addParam("wbAccount", wbAccount);
        ApiClient.getApiData(context, controller, methodName, pf, mUser);
    }

    public static void getVerify(MActivitySupport context, RequestCallback controller,
                                 String methodName, String phone) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MGetMobileVerify");
        MSystem.MRet ret = new MSystem.MRet();
        pf.addParam("phone", phone);
        ApiClient.getApiData(context, controller, methodName, pf, ret);
    }

    //注册及验证手机号
    public static void register(MActivitySupport context, RequestCallback controller,
                                String methodName, String phone, String code) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MRegister");
        MAppUser.MUser mUser = new MAppUser.MUser();
        pf.addParam("phone", phone);
        pf.addParam("code", code);
        pf.addParam("device", Android);
        ApiClient.getApiData(context, controller, methodName, pf, mUser, false);
    }

    //用户信息更新
    public static void updateUserInfo(MActivitySupport context, RequestCallback controller,
                                      String methodName, String password, String nickname, Integer sex) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MPasswdChange");
        MSystem.MRet ret = new MSystem.MRet();
        pf.addParam("password", MD5.md5(ConstantResource.MD5_KEY + password));
        pf.addParam("nickname", nickname);
        pf.addParam("sex", sex);
        ApiClient.getApiData(context, controller, methodName, pf, ret);
    }

    //获取笔记数量状态
    public static void getNoteCount(MActivitySupport context, RequestCallback controller,
                                    String methodName) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MQuesCountStatus");
        MAppQuestion.MQuestionCount ret = new MAppQuestion.MQuestionCount();
        ApiClient.getApiData(context, controller, methodName, pf, ret);
    }

    //同步课程名称信息，异常不抛出toast
    public static void updateCourseInfo(MActivitySupport context, RequestCallback controller,
                                        String methodName, BCourseList bCourseList) {
        if(bCourseList != null) {
            ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MUpdateSubject");
            MSystem.MRet ret = new MSystem.MRet();
            pf.addParam("subjectMath", bCourseList.getDisplayNameForUpdateWeb(3));
            pf.addParam("subjectMajor1", bCourseList.getDisplayNameForUpdateWeb(4));
            pf.addParam("subjectMajor2", bCourseList.getDisplayNameForUpdateWeb(5));
            pf.addParam("subjectEng", bCourseList.getDisplayNameForUpdateWeb(1));
            ApiClient.getApiData(context, controller, methodName, pf, ret, false);
        }
    }


    public static void collectEssence(MActivitySupport context, RequestCallback controller,
                                      String methodName, String id, int type) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MEssenceCollect");
        MSystem.MRet ret = new MSystem.MRet();
        pf.addParam("id", id);
        pf.addParam("type", type);
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);
    }

    /**
     * 设置用户信息
     *
     * @param context
     * @param controller
     * @param methodName
     * @param nickname
     * @param headimg
     * @param sex
     * @param email
     */
    public static void updateUserInfo(MActivitySupport context, RequestCallback controller,
                                      String methodName, String nickname, String headimg, Integer sex, String email) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MUpdateUserInfo");
        MSystem.MRet ret = new MSystem.MRet();
        pf.addParam("nickname", nickname);
        pf.addParam("headimg", headimg);
        pf.addParam("sex", sex);
        pf.addParam("email", email);
        ApiClient.getApiData(context, controller, methodName, pf, ret, true);

    }


    public static void sendEssenceEmail(MActivitySupport context, RequestCallback controller,
                                      String methodName, String id, String email, String resid, int isShared) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MEssenceDownload");
        MSystem.MRet ret = new MSystem.MRet();
        pf.addParam("id", id);
        pf.addParam("email", email);
        pf.addParam("resid", resid);
        pf.addParam("isShared", isShared);
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    //联系我们
    public static void contact(MActivitySupport context, RequestCallback controller,
                                        String methodName, String content, String contact) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MFeedback");
        MSystem.MRet ret = new MSystem.MRet();
        pf.addParam("content", content);
        pf.addParam("contact", contact);
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    //上传题目
    public static void uploadQuestion(MActivitySupport context, RequestCallback controller,
                               String methodName, MAppQuestion.MQuestion mQuestion) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MUploadQues");
        MSystem.MRet ret = new MSystem.MRet();
        pf.addParam("id", mQuestion.getId());
        pf.addParam("remark", mQuestion.getRemark());
        pf.addParam("type", mQuestion.getType());
        pf.addParam("img", mQuestion.getImg());
        pf.addParam("createTime", mQuestion.getCreateTime());
        pf.addParam("subject", mQuestion.getSubject());
        pf.addParam("isHighlight", mQuestion.getIsHighlight());
        pf.addParam("isRecommend", mQuestion.getIsRecommend());
        pf.addParam("hasLearned", mQuestion.getHasLearned());
        pf.addParam("reviewCount", mQuestion.getReviewCount());
        pf.addParam("status", mQuestion.getStatus() == 0 ? 1 : 0); //本地和服务器标志是反的， 服务器 1 正常 0 已删除
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    //今日推荐
    public static void todayRecommed(MActivitySupport context, RequestCallback controller,
                                      String methodName, String date) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MQuesRecommend");
        MAppQuestion.MQuestions ret = new  MAppQuestion.MQuestions();
        if(!StringUtil.isEmpty(date))
            pf.addParam("date", date);
        ApiClient.getApiData(context, controller, methodName, pf, ret, true);

    }

    //note print
    public static void printNote(MActivitySupport context, RequestCallback controller,
                                 String methodName, String startDate, String endDate, int type) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MQuesPrint");
        MSystem.MRet ret = new MSystem.MRet();
        pf.addParam("startDate", startDate);
        pf.addParam("endDate", endDate);
        pf.addParam("type", type);
        ApiClient.getApiData(context, controller, methodName, pf, ret, true);
    }

    public static void signCountStatus(MActivitySupport context, RequestCallback controller,
                                     String methodName, String date) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MSignCountStatus");
        if(StringUtil.isEmpty(date)) {
           date = DateUtil.getCurrentTime("yyyy-MM-dd");
        }
        pf.addParam("date", date);
        MAppQuestion.MQuestionCount ret = new  MAppQuestion.MQuestionCount();
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    public static void questionAllCount(MActivitySupport context, RequestCallback controller,
                                       String methodName) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MQuesCountStatusAll");
        MAppQuestion.MQuestionCount ret = new  MAppQuestion.MQuestionCount();
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    public static void uploadSign(MActivitySupport context, RequestCallback controller,
                                       String methodName, TrackByDay trackByDay) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MSign");
        if(StringUtil.isEmpty(trackByDay.getDay())) {
            trackByDay.setDay(DateUtil.getCurrentTime("yyyy-MM-dd"));;
        }
        String diary = "";
        if(!StringUtil.isEmpty(trackByDay.getDiary()) && !AppConfig.DEFAULT_DIARY_HINT.equals(trackByDay.getDiary()))
            diary = trackByDay.getDiary();
        pf.addParam("date", trackByDay.getDay());
        pf.addParam("diaryContent", diary);
        MSystem.MRet ret = new MSystem.MRet();
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    public static void indexs(MActivitySupport context, RequestCallback controller,
                                  String methodName, String day, int type, int days) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MIndex");
        if(StringUtil.isEmpty(day)) {
            day = DateUtil.getCurrentTime("yyyy-MM-dd");
        }
        pf.addParam("date", day);
        pf.addParam("type", type);
        pf.addParam("days", days);
        MSystem.MIndexs ret = new MSystem.MIndexs();
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    public static void essenceKeys(MActivitySupport context, RequestCallback controller,
                              String methodName, String key) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MRecommendKeys");
        pf.addParam("key", key);
        MRecommendKeys.MKeys ret = new MRecommendKeys.MKeys();
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    public static void questionDays(MActivitySupport context, RequestCallback controller,
                                    String methodName) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MGetDateList");
        MDays.MDates ret = new MDays.MDates();
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);

    }

    public static void questionListByDay(MActivitySupport context, RequestCallback controller,
                                         String methodName, String day) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MQuesListAll");
        pf.addParam("date", day);
        MAppQuestion.MQuestions ret = new MAppQuestion.MQuestions();
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);
    }

    public static void versionChecker(MActivitySupport context, RequestCallback controller,
                                         String methodName) {
        ParamsFormat pf = ParamsFormatFactory.getParamsFormat("MVersionCheck");
        MSystem.MVersionCheck ret = new MSystem.MVersionCheck();
        ApiClient.getApiData(context, controller, methodName, pf, ret, false);
    }
}
